package com.yanqu.road.server.cmd.banquet.party;

import com.yanqu.road.entity.banquet.UserBanquetFeastReward;
import com.yanqu.road.entity.enums.eBanquetType;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.banquet.BanquetModule;
import com.yanqu.road.server.pb.BanquetPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Map;

@Cmd(code = Protocol.S_CROSS_PARTY_FEAST_REWARD, desc = "")
public class CrossGetFeastRewardResultCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        BanquetProto.CrossFeastGetRewardRespMsg msg = BanquetProto.CrossFeastGetRewardRespMsg.parseFrom(packet.getBytes());
        ret = msg.getRet();
        int type = 0;
        if (ret == 0) {
            int rewardId = msg.getRewardId();
            String partyId = msg.getPartyId();
            ret = player.getModule(BanquetModule.class).checkFeastReward(partyId, rewardId,type);
            if (ret == 0) {
                player.getModule(BanquetModule.class).addFeastReward(partyId, rewardId,type);
            }
            try {
                Map<String, Map<Integer, UserBanquetFeastReward>> banquetRewardMap = player.getModule(BanquetModule.class).getUserBanquetFeastRewardList();
                BanquetProto.BanquetPopRewardSync.Builder respMsg = BanquetPb.parseBanquetFeastRewardListResp(banquetRewardMap, eBanquetType.ServerFeastBanquet.getValue(), true);
                player.sendPacket(Protocol.U_BANQUET_FEAST_REWARD_SYNC, respMsg);
            } catch (Exception e) {
                log.error("sync banquet error",e);
            }
        }

    }

    @Override
    public void sendData(GamePlayer player) {
        BanquetProto.BanquetGetPopRewardResp.Builder respMsg = BanquetProto.BanquetGetPopRewardResp.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(Protocol.U_BANQUET_FEAST_GET_REWARD, respMsg);
    }
}
